<?php /* -*- tab-width: 4; coding: utf-8 -*-
vim: ts=4 noet ai */

/*
	Copyright (C) 2016 Desktopd Project

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function onion_connect ($socks, $hostname, $port, $timeout = 50) {
    if (!is_string($hostname)) {
        throw new ErrorException('Invalid hostname');
    } elseif (!preg_match('/^[a-z2-7]{16,249}\.onion$/', $hostname)) {
        throw new ErrorException('Unsupported hostname');
    }
    
    
    $port = (int) $port;
    if ($port < 1 || 65535 < $port) {
        throw new ErrorException('Invalid port');
    }
    
    $stream = stream_socket_client("tcp://$socks", $errno, $errstr, $timeout);
    if (!is_resource($stream)) {
        throw new RuntimeException("Error connecting the SOCKS server: $errstr ($errno)");
    }
    
    fwrite($stream, "\x05\x01\x00");
    $response = fread($stream, 2);
    if ("\x05\x00" !== $response) {
        @fclose($stream);
        throw new RuntimeException("Server refused the negotiation");
    }
    
    
    // connection request
    $hostnameLength = pack('C', strlen($hostname));
    $port = pack('n', $port);
    fwrite($stream, "\x05\x01\x00\x03{$hostnameLength}{$hostname}{$port}");
    
    $version = fread($stream, 1);
    if ("\x05" !== $version) {
        @fclose($stream);
        $dumped = is_string($version) ? bin2hex($version) : '?';
        throw new RuntimeException("Version error: $dumped");
    }
    
    
    $status = fread($stream, 1);
    if ("\x00" !== $status) {
        @fclose($stream);
        switch ($status) {
            case "\x01": throw new RuntimeException("ERROR: General SOCKS server failure");
            case "\x02": throw new RuntimeException("ERROR: Connection not allowed by ruleset");
            case "\x03": throw new RuntimeException("ERROR: Network unreachable");
            case "\x04": throw new RuntimeException("ERROR: Host unreachable");
            case "\x05": throw new RuntimeException("ERROR: Connection refused");
            case "\x06": throw new RuntimeException("ERROR: TTL expired");
            case "\x07": throw new RuntimeException("ERROR: Command not supported");
            case "\x08": throw new RuntimeException("ERROR: Address type not supported");
            default: throw new RuntimeException("ERROR: Unknown status");
        }
        
        throw new RuntimeException("Unknown error");
    }
    
    $reserved = fread($stream, 1);
    if ("\x00" !== $reserved) {
        throw new RuntimeException("Server violated the protocol");
    }
    
    $address_type = fread($stream, 1);
    if ("\x01" === $address_type) {
        // IPv4
        $bind_address = fread($stream, 4);
    } elseif ("\x04" === $address_type) {
        // IPv6
        $bind_address = fread($stream, 16);
    } else {
        @fclose($stream);
        throw new RuntimeException("Server returned an unsupported address");
    }
    
    $bind_port = fread($stream, 2);
    if (is_string($bind_port) && 2 === strlen($bind_port)) {
        return $stream;
    }
    
    throw new RuntimeException("Server returned an invalid port");
}


